package com.spider.silence.utils;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import sun.net.ftp.FtpClient;

import java.io.IOException;

/**
 * Created by 夏茂轩 on 2017/1/9.
 */
public class FtpUtil {
    private Logger logger =Logger.getLogger(FtpUtil.class);

    private static FtpUtil ourInstance = new FtpUtil();

    /**
     * @author 夏茂轩
     * @date 2017/1/15 22:37
     * @description 获取规格化后的 FTPClient
     * @return org.apache.commons.net.ftp.FTPClient
     */
    public static synchronized FTPClient getClient() {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(PropertiesUtil.getVal("main.ftp.outer.ip"));
            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();
                ourInstance.logger.error("FTP登录错误");
                System.exit(1);
            }
            ftpClient.setControlEncoding("UTF-8");
            boolean flag=ftpClient.login("silence", "silence");
            while (!flag){
                flag=ftpClient.login("silence", "silence");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  ftpClient;
    }

    private FtpUtil() {
    }
}
